package com.hllxd.hism.star.basic.service.service;


import com.alibaba.fastjson.JSONObject;
import com.hllxd.hism.star.basic.service.collect.CollectYarn;
import com.hllxd.hism.star.basic.service.collect.OkHttpUtil;
import com.hllxd.hism.star.basic.service.dao.mapper.mis.TimeoutTaskMngMapper;
import com.hllxd.hism.star.basic.service.model.TimeoutTaskEntity;
import com.hllxd.hism.star.basic.service.model.rock.YarnApplicationEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;


@Slf4j
@Service("timeoutTaskMngService")
public class TimeoutTaskMngServiceImpl implements TimeoutTaskMngService {

    @Autowired
    TimeoutTaskMngMapper timeoutTaskMngMapper;

    @Autowired
    private YarnApplicationService yarnApplicationService;

    private static final String APP_API = "/ws/v1/cluster/apps/%s";

    @Override
    public List<TimeoutTaskEntity> listTimeoutTask(HashMap<String, String> paramMap) {

        List<TimeoutTaskEntity> timeoutTasks = timeoutTaskMngMapper.listWeekTimeoutTask(paramMap);
        for (TimeoutTaskEntity timeoutTask : timeoutTasks) {
            String appId = timeoutTask.getAppId();

            String api = String.format(APP_API, appId);
            try {
                String request = CollectYarn.YARN_URL + api;
                String res = OkHttpUtil.getDataString(request);
                if (res.contains("RemoteException")) {
                    continue;
                }
                JSONObject json = JSONObject.parseObject(res);
                YarnApplicationEntity yarnApplication = json.getObject("app", YarnApplicationEntity.class);
                timeoutTask.setProgress(yarnApplication.getProgress());
                timeoutTask.setTrackingUrl(yarnApplication.getTrackingUrl());
                timeoutTask.setSubmitTime(yarnApplication.getStartedTime());
                timeoutTask.setLaunchTime(yarnApplication.getLaunchTime());
                timeoutTask.setFinishedTime(yarnApplication.getFinishedTime());
                if (yarnApplication.getFinishedTime() != null) {
                    if (yarnApplication.getFinishedTime() <= 0) {
                        timeoutTask.setUseTime(System.currentTimeMillis() - yarnApplication.getStartedTime());
                    } else {
                        timeoutTask.setUseTime(yarnApplication.getFinishedTime() - yarnApplication.getStartedTime());
                    }
                }
                timeoutTask.setContainer(yarnApplication.getRunningContainers());
                timeoutTask.setAllocatedVCores(yarnApplication.getAllocatedVCores());
                timeoutTask.setAllocatedMB(yarnApplication.getAllocatedMB());
                timeoutTask.setQueue(yarnApplication.getQueue());
                timeoutTask.setUser(yarnApplication.getUser());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return timeoutTasks;
    }

    @Override
    public int setDelayKillConfig(Long[] ids, Long delayTime) {
        return timeoutTaskMngMapper.updateDelayKillConfig(ids, delayTime);
    }

    @Override
    public boolean killTimeoutTask(String appId) {
        log.info("start kill app:{}", appId);
        yarnApplicationService.killYarnApplication(appId);
        timeoutTaskMngMapper.updateTimeoutTaskActionByAppId(appId);
        return true;
    }
}
